package org.jetlinks.pro.assets;

import lombok.*;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@Getter
@Setter
@AllArgsConstructor(staticName = "of")
@NoArgsConstructor
@EqualsAndHashCode
public class DimensionInfo implements Serializable {
    private static final long serialVersionUID = -6849794470754667710L;

    private String type;

    private String id;

    public static Optional<DimensionInfo> of(Map<String, Object> val) {
        if (val == null) {
            return Optional.empty();
        }
        if (!val.containsKey("type") || !val.containsKey("id")) {
            return Optional.empty();
        }
        return Optional.of(
            DimensionInfo.of(String.valueOf(val.get("type")),
                             String.valueOf(val.get("id")))
        );
    }

    public String getKey() {
        return AssetsUtils.createBindingTargetKey(type, id);
    }

    public Map<String,Object> toMap(){
        Map<String, Object> map = new HashMap<>();
        map.put("type", type);
        map.put("id", id);
        return map;
    }
}
